<template>
  <div class="app-container">
    <div class="table-container">
      <div class="left">
        <to-link :val="'YdTHyHh0i'" @to="handleOpen">
          <el-button size="small" type="primary">添加</el-button>
        </to-link>
      </div>
      <div class="right">
        <el-radio-group v-model="search_type" size="small" @change="search_type_value">
          <el-radio-button border :key="`0`" :label="`0`">全部</el-radio-button>
          <el-radio-button border v-for="item in ad_type" :key="item.value" :label="item.value">{{item.label}}</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="_list">
      <el-table highlight-current-row @selection-change="handleSelectionChange" :data="dataList" style="width: 100%" v-loading.body="listLoading">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="门店名称" width="380">
          <template slot-scope="scope">
            {{scope.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="类型" width="180">
          <template slot-scope="scope">
            {{scope.row.stores_stype === '1' ? '直营店' : '加盟店'}}
          </template>
        </el-table-column>
        <el-table-column label="门店地址">
          <template slot-scope="scope">
            {{scope.row.address}}
          </template>
        </el-table-column>
        <el-table-column label="联系方式">
          <template slot-scope="scope">
            {{scope.row.tel}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <to-link :val="'uhBRcD0LF'" @to="_handleUpdata(scope.row)">
              <el-button size="small" type="text">编辑</el-button>
            </to-link>
            <to-link :val="'2LkG9dD9-'" @to="handleDelete(scope.row)">
              <el-button size="small" type="text">删除</el-button>
            </to-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pages">
      <div class="left">
        <to-link :val="'PmEc3qVnn'" @to="handleAll">
          <el-tooltip class='item' effect='dark' content='批量删除' placement='top'>
            <el-button size="small" type="danger">批量删除</el-button>
          </el-tooltip>
        </to-link>
      </div>
      <div class="right" v-if="total > 0">
        <el-pagination @current-change="handleCurrentChange" background :current-page.sync="page" layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="门店添加" :visible.sync="dialogVisible" width="450px" top="5vh">
      <el-form ref="dialogForm" :model="form" :rules="dialogFormRules" label-width="80px">
      	<el-form-item label="门店类型" prop="stores_stype">
          <el-select v-model="form.stores_type" placeholder="请选择" @change="getAddress">
            <el-option v-for="item in ad_type" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="门店地址" prop="address_id">
          <el-select v-model="form.address_id" placeholder="请选择">
            <el-option v-for="item in addressList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店地址" prop="address">
          <el-input type="input" v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input type="input" v-model="form.tel"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import Service from '@/api/service'
import mixinsTable from '@/utils/mixins.table'
import Upload from '@/components/Upload'
export default {
  name: 'storesList',
  mixins: [mixinsTable],
  data() {
    return {
      servicePath: '/pages/stores',
      ad_type: [{
        value: '1',
        label: '直营店'
      }, {
        value: '2',
        label: '加盟店'
      }],
      search_type: '0',
      addressList: [],
      form: {
        name: '',
        address_id: '',
        address: '',
        stores_stype: '',
        tel: ''
      },
      dialogFormRules: {
        stores_type: [
          { required: true, message: '门店类型不能为空', trigger: 'change' }
        ],
        name: [
          { required: true, message: '门店名称不能为空', trigger: 'blur' }
        ],
        address_id: [
          { required: true, message: '门店地址不能为空', trigger: 'change' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    Upload
  },
  computed: {
    addressService() {
      return new Service('/pages/address')
    }
  },
  created() {
    this.getOptionList()
    this.getList()
  },
  sockets: {
    connect() {
      console.log('socket connect')
    },
    news(val) {
      this.$notify.success(val)
    }
  },
  methods: {
    initForm(row) {
      this.setFormVal(row)
    },
    _handleUpdata(row) {
      this.getAddress()
      this.handleUpdata(row)
    },
    FormClear() {
      this.setFormVal(this.form)
    },
    setFormVal(row) {
      const formArr = Object.keys(row)
      formArr.map(item => {
        this.$set(this.form, item, this.dialogState === 'update' ? row[item] : '')
      })
    },
    getAddress() {
      this.addressService.list({ stores_type: this.form.ad_type }).then(res => {
        this.addressList = res.data.result.map(item => {
          return {
            value: item._id,
            label: item.name
          }
        })
      })
    },
    handleCurrentChange(val) {
      this.getList(val)
    },
    upload_success(val) {
      this.$set(this.form, 'img_id', val.mid)
    },
    search_type_value(val) {
      if (val !== '0') {
        this.$set(this.params, 'ad_type', val)
      } else {
        delete this.params.ad_type
      }
      this.getList()
    }
  }
}

</script>
<style lang="scss" scoped>


</style>
